import request from '@/utils/request'

// 查询商户列表
export function merchantPage(query) {
  return request({
    url: '/manage/merchant/page',
    method: 'get',
    params: query
  })
}

// 商户列表新增
export function merchantAdd(data) {
  return request({
    url: '/manage/merchant/add',
    method: 'post',
    data
  })
}
// 修改商户费率
export function changeRate(data) {
  return request({
    url: '/manage/merchant/resetMerchantRate',
    method: 'post',
    data
  })
}
// 获取商家明细
export function getRate(query) {
  return request({
    url: '/merchant/getRate',
    method: 'get',
    params: query
  })
}

// 获取余额详情分页
export function recordPage(data) {
  return request({
    url: '/merchant/balance/record/page',
    method: 'post',
    data
  })
}
// 切换状态
export function changeUse(data) {
  return request({
    url: '/manage/merchant/operationMerchantUse',
    method: 'post',
    data
  })
}
// 开通Api
export function openApi(data) {
  return request({
    url: '/manage/merchant/openApiServer',
    method: 'post',
    data
  })
}
// 下载加密key

export function loadKey(query) {
  return request({
    url: '/merchant/merchant/openApiServer',
    method: 'get',
    params: query
  })
}

export function downloadKey(merchantNo) {
  const url = '/merchant/merchant/openApiServer';
  const params = { merchantNo : merchantNo };

  // 显示加载状态
  document.getElementById('downloadButton').disabled = true;

  request({
    url,
    method: 'GET',
    params,
    responseType: 'blob',
  }).then(response => {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'keyfile.key');
    document.body.appendChild(link);
    link.click();
    link.remove();
    window.URL.revokeObjectURL(url);

    // 恢复按钮状态
    document.getElementById('downloadButton').disabled = false;
  }).catch(error => {
    console.error('下载文件失败:', error);
    alert('下载文件失败，请稍后再试。');

    // 恢复按钮状态
    document.getElementById('downloadButton').disabled = false;
  });
}
// 重置商户登录密码

export function resetPassword(data) {
  return request({
    url: '/manage/user/resetPassword',
    method: 'post',
    data
  })
}

//配置机器人
export function setBotConfig(data) {
  return request({
    url: '/manage/merchant/setBotConfig',
    method: 'post',
    data
  })
}
// 修改金额

export function changeAmount(data) {
  return request({
    url: '/manage/change/balance',
    method: 'post',
    data
  })
}